package risoe.syslab.fhnew.hw.enocean;

/*
 * Copyright (c) 2012-2014, Technical University of Denmark (DTU)
 * All rights reserved.
 * 
 * The Flexhouse 2.0 platform ("this software") is licensed under the
 * BSD 3-clause license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of DTU nor the names of its contributors may be used to
 *     endorse or promote products derived from this software without specific
 *     prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE TECHNICAL UNIVERSITY OF DENMARK BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import risoe.syslab.model.CompositeMeasurement;
import risoe.syslab.model.Quality;
import risoe.syslab.model.Source;
import risoe.syslab.model.Validity;

/**
 * Message from sensors of the type SR65LI.
 *
 * Description Radio Telegram:
 * <ul>
 * <li> ORG 7 dec. Always (EnOcean module type 4BS)
 * <li> Data_byte2 Brightness value 300.30000 Lux linear n=0...255
 * <li> Data_byte1 Brightness value 600.60000 Lux linear n=0...255
 * <li> Data_byte0 Bit D3 Learn button (pressed=0)
 * <li> ID_Byte3 device identifier (Byte3)
 * <li> ID_Byte2 device identifier (Byte2)
 * <li> ID_Byte1 device identifier (Byte1)
 * <li> ID_Byte0 device identifier (Byte0)
 * </ul>
 * @author daku
 */
public class LightMessage extends EnoceanRRT4BSMessage {
  
  public LightMessage(char[] msgBuf) {
    super(msgBuf);
  }

  public float getLux() {
    // Assuming sensitivity between 300 and 30000 Lux.
    float d=(float)messageData[2];
    float START = 300.0f;
    float END = 30000.0f;
// This does not work, because it's impossible to decide between 300 and 600 Lux when
// the message contains two zero bytes in the payload:
//    if (messageData[1] != 0) {
//      // Assumption may be wrong
//      // Assuming sensitivity between 600 and 60000 Lux.
//      d=(float)messageData[1];
//      START = 600.0f;
//      END = 60000.0f;
//    }
    float value = START + (END-START) / 255 * d;
    return value;
  }

  public boolean isLearnButtonPressed() {
    return ((messageData[0] & 0x08)==0);
  }

  public String toString() {
    StringBuffer rv=new StringBuffer();
    rv.append(this.getClass().getName());
    rv.append("[");
    rv.append("L="+getLux()+" Lux");
    rv.append("]");
    return rv.toString();
  }

//  /** Used for basic testing of logic */ 
//  public static void main(String[] args) {
//    System.out.println(new IndoorLightMessage(new char[] {0xb, 0x7, 0, 0, 0, 0, 0, 0x11, 0x22, 0x33, 0x44}));
//    System.out.println(new IndoorLightMessage(new char[] {0xb, 0x7, 0, 255, 0, 0, 0, 0x11, 0x22, 0x33, 0x44}));
//    System.out.println(new IndoorLightMessage(new char[] {0xb, 0x7, 0, 0, 255, 0, 0, 0x11, 0x22, 0x33, 0x44}));
//    System.out.println(new IndoorLightMessage(new char[] {0xb, 0x7, 0, 255, 255, 0, 0, 0x11, 0x22, 0x33, 0x44}));
//    System.out.println(new IndoorLightMessage(new char[] {0xb, 0x7, 0, 127, 0, 0, 0, 0x11, 0x22, 0x33, 0x44}));
//    System.out.println(new IndoorLightMessage(new char[] {0xb, 0x7, 0, 128, 0, 0, 0, 0x11, 0x22, 0x33, 0x44}));
//  }

  public CompositeMeasurement getValue(int channel) {
    double rv=Double.NaN;
    if (channel==1)
      rv=getLux();
    return new CompositeMeasurement(rv, Quality.OK, Validity.GOOD, Source.PROCESS);
  }  
}
